﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;

public partial class Modules_EnfermedadesControlEstanque_PlanesDeControl : System.Web.UI.Page
{
    private int idEstanque
    {
        get{return Convert.ToInt32(ViewState["idestanque"]);}
        set { ViewState["idestanque"] = value; }

    }

    private PlanControl PlanControl
    {
        get { return (PlanControl) ViewState["plancontrol"]; }
        set { ViewState["plancontrol"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            Globals_GenericMasterPageNoSecurity master = (Globals_GenericMasterPageNoSecurity)this.Master;
            master.Imagen = "../Styles/Icons/schedule.png";
            master.Titulo = "Plan de control actual";

            this.idEstanque = Convert.ToInt32(Request["idEstanque"]);
        }
        //InicializarGrilla();
        CargarDatos();
    }

    private void InicializarGrilla()
    {
        //tdgPlanificacion.setupGrid(typeof(ItemDetallePlanControl), null, false, false, this.ViewState);
    }

    private void CargarDatos()
    {
        PlanControl.getManager.ClearFilters();
        PlanControl.getManager.ClearOrders();
        PlanControl.getManager.AddFilter("estanque", BussinessLogicTecnoPez.GenericManager<PlanControl>.filterOperator.Equals, this.idEstanque);
        PlanControl.getManager.AddOrder("codigo", BussinessLogicTecnoPez.GenericManager<PlanControl>.Order.orderDirection.DESC);

        List<PlanControl> lista= PlanControl.getManager.GetFilter();

        if (lista.Count > 0)
        {
            PlanControl datos = lista[0];
            this.PlanControl = datos;

            if (datos.DetallePlanControl.ItemList.Count > 0)
            {
                this.txtEmpleado.Text = datos.Empleado.ToString();
                this.txtEstanque.Text = datos.Estanque.ToString();
                this.txtFechaFin.Text = datos.FechaFin.ToShortDateString();
                this.txtFechaInicio.Text = datos.FechaInicio.ToShortDateString();

                datos.DetallePlanControl.ItemList.Sort();

                this.tdgPlanificacion.DataSource = datos.DetallePlanControl;
                this.tdgPlanificacion.DataBind();

                divArriba.Visible = true;
                lblGeneracion.Text = "Volver a generar plan";
            }
            else
            {
                divArriba.Visible = false;
                lblGeneracion.Text = "Generar plan";
            }
        }
        else
        {
            divArriba.Visible = false;
            lblGeneracion.Text = "Generar plan";
        }
    }
    protected void cmdBuscar_Click(object sender, EventArgs e)
    {
        Response.Redirect("GenerarPlanControl.aspx?idEstanque=" + this.idEstanque);
    }

    protected void tdgPlanificacion_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (DataBinder.GetPropertyValue(e.Row.DataItem, "Control") == null)
            {
                ParametroControl pcontrol = (ParametroControl)DataBinder.GetPropertyValue(e.Row.DataItem, "ParametroControl");
                int IdPlanControl = this.PlanControl.Codigo;
                DateTime fecha = Convert.ToDateTime(DataBinder.GetPropertyValue(e.Row.DataItem, "Fecha"));
                string url = "InsertarControl.aspx?idestanque=" + this.idEstanque.ToString() + "&idparametro=" + pcontrol.IdParametroControl.ToString() + "&idPlanControl=" + IdPlanControl.ToString() + "&fecha=" + fecha.ToString();
                string strparam = " width= 550,height= 400, scrollbars=NO, resizable=yes, top=200,left=300,status=yes ";
                ((ImageButton)e.Row.Cells[5].Controls[0]).OnClientClick = "javascript:window.open('" + url + "','InsertarControl', '" + strparam + "'); return false;";
                ((ImageButton)e.Row.Cells[5].Controls[0]).ToolTip = "Insertar control";
            }
            else
            {
                //examinar
                Controles control = (Controles) DataBinder.GetPropertyValue(e.Row.DataItem, "Control");
                string url = "VerControl.aspx?idControl=" + control.IdControl.ToString();
                string strparam = " width= 550,height= 400, scrollbars=NO, resizable=yes, top=200,left=300,status=yes ";
                ((ImageButton)e.Row.Cells[5].Controls[0]).ImageUrl = "~/Styles/Icons/examinar.png";
                ((ImageButton)e.Row.Cells[5].Controls[0]).ToolTip = "Ver control realizado";
                ((ImageButton)e.Row.Cells[5].Controls[0]).OnClientClick = "javascript:window.open('" + url + "','InsertarControl', '" + strparam + "'); return false;";
            }
        }
    }
    protected void btnImprimir_Click(object sender, EventArgs e)
    {
        WebUtils.Controls.visorDeReportes objVisor = new WebUtils.Controls.visorDeReportes();

        objVisor.archivoReporte = new rptPlanControl();
        objVisor.selectionFormula = "{ESTANQUE.IDESTANQUE} = " + this.idEstanque.ToString() + " AND {PLANESCONTROL.IDPLANCONTROL} =" + this.PlanControl.Codigo.ToString();
        objVisor.verReporte(this);
    }
}
